package cn.getech.data.development.utils

import java.util.Properties

import org.slf4j.{Logger, LoggerFactory}

object ConfigurationManager {
  lazy val props = new Properties()
  private val logger: Logger = LoggerFactory.getLogger(this.getClass)

  try {
    props.load(ConfigurationManager.getClass.getClassLoader.getResourceAsStream("conf.properties"))
  } catch {
    case e: Exception => e.printStackTrace()
  }
  def getOrDefault(key: String, other: String): String = {
    props.getOrDefault(key, other).toString
  }

  /**
   * 获取指定key对应的value
   *
   * @param key
   * @return
   */
  def getProperty(key: String): String = {
    props.getProperty(key)
  }

  def getInteger(key: String): Int = {
    val value = getProperty(key)
    try
      return value.toInt
    catch {
      case e: Exception => logger.error("数据类型不是Int！！", e.getMessage)
    }
    0
  }

  /**
   * 获取布尔类型的配置项
   *
   * @param key
   * @return value
   */
  def getBoolean(key: String): Boolean = {
    val value = getProperty(key)
    try
      return value.toBoolean
    catch {
      case e: Exception => logger.error("数据类型不是Boolean！！", e.getMessage)
    }
    false
  }

  /**
   * 获取Long类型的配置项
   *
   * @param key
   * @return
   */
  def getLong(key: String): Long = {
    val value = getProperty(key)
    try
      return value.toLong
    catch {
      case e: Exception => logger.error("数据类型不是Long！！", e.getMessage)
    }
    0L
  }

}
